<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="第四章 数组4.1 数组 4.1.1数组的概念  数组是一种引用数据类型 数组要经过定义分配内存及赋值后才能使用   4.1.2 数组的定义   一维数组  定义  有一个下表的数组 int Array[]&#x3D;{1,2,3};&#x2F;&#x2F;定义一个整型数组String Array[]&#x3D;{1,2,3};&#x2F;&#x2F;定义一个字符串数组   创建  直接赋值 用new建立数组   引用  []        多维数组  定">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%9B%9B%E7%AB%A0%20%E6%95%B0%E7%BB%84/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="第四章 数组4.1 数组 4.1.1数组的概念  数组是一种引用数据类型 数组要经过定义分配内存及赋值后才能使用   4.1.2 数组的定义   一维数组  定义  有一个下表的数组 int Array[]&#x3D;{1,2,3};&#x2F;&#x2F;定义一个整型数组String Array[]&#x3D;{1,2,3};&#x2F;&#x2F;定义一个字符串数组   创建  直接赋值 用new建立数组   引用  []        多维数组  定">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-12-19T17:27:36.978Z">
<meta property="article:modified_time" content="2021-12-19T17:27:37.824Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/notes-on-computer-expertise/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/notes-on-computer-expertise/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/notes-on-computer-expertise/css/style.css">

  
    
<link rel="stylesheet" href="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/notes-on-computer-expertise/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/">Home</a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/notes-on-computer-expertise/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://goofyer.gitee.io/notes-on-computer-expertise"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Java/第四章 数组" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%9B%9B%E7%AB%A0%20%E6%95%B0%E7%BB%84/" class="article-date">
  <time class="dt-published" datetime="2021-12-19T17:27:36.978Z" itemprop="datePublished">2021-12-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="第四章-数组"><a href="#第四章-数组" class="headerlink" title="第四章 数组"></a>第四章 数组</h2><h3 id="4-1-数组"><a href="#4-1-数组" class="headerlink" title="4.1 数组"></a>4.1 数组</h3><ul>
<li><p>4.1.1数组的概念</p>
<ul>
<li>数组是一种引用数据类型</li>
<li>数组要经过定义分配内存及赋值后才能使用</li>
</ul>
</li>
<li><p>4.1.2 数组的定义</p>
<ul>
<li><ol>
<li><p>一维数组</p>
<ul>
<li><p>定义</p>
<ul>
<li>有一个下表的数组</li>
<li>int Array[]={1,2,3};//定义一个整型数组<br>String Array[]={1,2,3};//定义一个字符串数组</li>
</ul>
</li>
<li><p>创建</p>
<ul>
<li>直接赋值</li>
<li>用new建立数组</li>
</ul>
</li>
<li><p>引用</p>
<ul>
<li>[]</li>
</ul>
</li>
</ul>
</li>
</ol>
</li>
<li><ol>
<li><p>多维数组</p>
<ul>
<li><p>定义</p>
<ul>
<li>类型 数据名[][]</li>
</ul>
</li>
<li><p>创建</p>
<ul>
<li>直接赋值</li>
<li>new</li>
</ul>
</li>
<li><p>引用</p>
<ul>
<li>[][]</li>
</ul>
</li>
</ul>
</li>
</ol>
</li>
</ul>
</li>
<li><p>案例</p>
<ul>
<li><p>成绩排序和统计</p>
<ul>
<li><p>技术要点</p>
<ul>
<li>单独定义一个类来处理成绩问题，在该类中用数组存放成绩，在构造方法中根据分数创建数组</li>
</ul>
</li>
<li><p>代码</p>
<p>/<em><br>设计程序 能对学生成绩进行统计 并把各个分数段的人数统计出来
 </em>/<br>public class 成绩排序和统计 {</p>
<pre><code>private float scores[];

public 成绩排序和统计(int n) &#123;
    scores = new float[n];
&#125;

//输入成绩
public void input() &#123;
    for (int i = 0; i &lt; scores.length; i++) &#123;
        Scanner in = new Scanner(System.in);
        scores[i] = in.nextFloat();
    &#125;
&#125;

//输出成绩
public void output() &#123;
    for (int i = 0; i &lt; scores.length; i++) &#123;
        System.out.println(scores[i]);
        if (i &lt; scores.length - 1)
            System.out.println(&#39;,&#39;);
    &#125;
&#125;//从高到低排序

public sort(int n) &#123;
    for (int i = 0; i &lt; scores.length; i++) &#123;
        for (int j = i + 1; j &lt; scores.length; j++) &#123;
            if (scores[i] &lt; scores[j]) &#123;
                float c = scores[i];
                scores[i] = scores[j];
                scores[j] = c;
            &#125;
        &#125;
    &#125;
&#125;//统计成绩

public void stat(int n) &#123;
    int s[] = new int[11];
    for (int i = 0; i &lt; scores.length; i++) &#123;
        int j = (int) (scores[i] / 10);
        s[j]++;
        scores = new float[n];
    &#125;
    for (int i = 0; i &lt; s.length; i++) &#123;
        System.out.println(&quot;#%3s~%3s:%d&quot;, i * 10, (i + 1) * 10 - 1, s[i]);
    &#125;
&#125;
</code></pre><p>}</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="4-2集合"><a href="#4-2集合" class="headerlink" title="4.2集合"></a>4.2集合</h3><ul>
<li><p>4.2.1java集合框架</p>
<ul>
<li>util包</li>
</ul>
</li>
<li><p>4.2.2Collection接口常用方法</p>
<ul>
<li>a.add(1);<br>a.contains(a);<br>a.isEmpty();<br>a.iterator();<br>a.remove(3);<br>a.size();</li>
</ul>
</li>
<li><p>4.2.3遍历Collection</p>
<ul>
<li>for循环</li>
<li>迭代器</li>
</ul>
</li>
</ul>
<h3 id="4-3集"><a href="#4-3集" class="headerlink" title="4.3集"></a>4.3集</h3><ul>
<li><p>HashSet类</p>
<ul>
<li>哈希表</li>
</ul>
</li>
<li><p>TreeSet类</p>
</li>
</ul>
<h3 id="4-4列表"><a href="#4-4列表" class="headerlink" title="4.4列表"></a>4.4列表</h3><ul>
<li><p>序的集合，可以精确控制每个元素插入的位置</p>
<ul>
<li>索引</li>
<li>可以重复元素</li>
</ul>
</li>
<li><p>List接口</p>
<ul>
<li><p>ArrayList类</p>
<ul>
<li>可变大小的列表</li>
</ul>
</li>
<li><p>案例</p>
<ul>
<li><p>竞赛评分程序</p>
<ul>
<li><p>技术要点</p>
<ul>
<li>利用Arraylist存储选手对象，使用工具类Collect的Sort方法队选手进行排序，采用匿名类方法定义比较规则</li>
</ul>
</li>
<li><p>代码</p>
<ul>
<li><p>创建对象类</p>
<p>public class Player {</p>
<pre><code>private final String playerId;//选手号码
private final String playerName;//选手名称
</code></pre><p>private  int order;//比赛次序<br> private float scores[];   //选手分数列表</p>
<pre><code>public Player(String playerId,String playerName)&#123;
    this.playerId=playerId;
    this.playerName=playerName;
&#125;
public String getPlayerId()
&#123;return playerId;&#125;
public String getPlayerName()
&#123;return playerName;&#125;
public int getOrder()
&#123;return order;&#125;
public void setOrder(int order)
&#123;this.order=order;&#125;
public void inputScore(int judgeNum)
&#123;
    this.scores=new float[judgeNum];
    Scanner in=new Scanner(System.in);
    System.out.println(&quot;输入&quot;+playerId+&quot;号选手的得分&quot;);
    for(int i=0;i&lt;judgeNum;i++)
        scores[i]=in.nextFloat();
&#125;
public float getScore()
&#123;
    float totalScore=0f;
    int mini=0;
    int maxi=0;
    for(int i=1;i&lt;scores.length;i++)
    &#123;
        totalScore+=scores[i];
        if(scores[i]&gt;scores[maxi])
            maxi=i;
        if(scores[i]&lt;scores[mini])
            mini=i;
    &#125;
    totalScore=(totalScore-scores[maxi]-scores[mini])/(scores.length-2);
&#125;
@Override
public String toString()
&#123;
    return String.format(&quot;%!$-10s%2$-15s%3$-10.2f&quot;,playerId,playerName,getScore())
&#125;
</code></pre><p>}</p>
</li>
<li><p>CMSystem类</p>
<p>public class CMSystem {</p>
<pre><code>private List&lt;Player&gt; playerList;

public CMSystem() &#123;
    this.playerList = new ArrayList&lt;&gt;();
&#125;

public void addPlayer(Player player) &#123;
    playerList.add(player);
&#125;

public void deletePlayer(int index) &#123;
    playerList.remove(index);
&#125;

public void sortByScore() &#123;
    Collections.sort(playerList, new Comparator&lt;Player&gt;()//匿名类
    &#123;
        @Override
        public int compare(Player o1, Player o2) &#123;
            if (o1.getScore() &lt;= o2.getScore())
                return 1;
            else
                return -1;
        &#125;
    &#125;);
&#125;

public void sortByOrder() &#123;
    Collections.sort(playerList, new Comparator&lt;Player&gt;()//匿名类
    &#123;
        @Override
        public int compare(Player o1, Player o2) &#123;
            if (o1.getOrder() &lt;= o2.getOrder())
                return 1;
            else
                return -1;
        &#125;
    &#125;);
&#125;

public void inputScore(int judgeNum) &#123;
    sortByOrder();
    for (Player p : playerList)
        p.inputScore(judgeNum);
&#125;

public void showAllPlayerInfo() &#123;
    for (Player p : playerList)
        System.out.println(p);
&#125;
</code></pre><p>}</p>
</li>
<li><p>子主题 3</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><p>Vector类</p>
</li>
</ul>
<h3 id="4-5映射"><a href="#4-5映射" class="headerlink" title="4.5映射"></a>4.5映射</h3><ul>
<li><p>定义</p>
<ul>
<li>映射是由键值对组成 Key不能重复，每对key只能映射一个value</li>
</ul>
</li>
</ul>
<h3 id="4-5-1Map接口"><a href="#4-5-1Map接口" class="headerlink" title="4.5.1Map接口"></a>4.5.1Map接口</h3><ul>
<li>独立接口，不继承Collection接口</li>
<li><p>方法</p>
<ul>
<li>clear()</li>
<li>containsKey()</li>
<li>containsValue()</li>
<li>entrySet()</li>
<li>get()</li>
<li>isEmpty()</li>
<li>keySet()</li>
<li>put()</li>
<li>remove()</li>
<li>size()</li>
<li>values()</li>
</ul>
</li>
<li><p>HashMap类</p>
</li>
<li><p>4.3案例 网络书城的购物车类</p>
<ul>
<li><p>技术要点</p>
<ul>
<li>定义一个图书类Book，存书的信息</li>
<li>定义一个条目类CartItem表示购物车的单个条目，存储书及数量</li>
<li>定义一个购物车类Cart，用HashMap存储数目</li>
<li>使用containKey方法判断书是否在购物车中</li>
</ul>
</li>
<li><p>代码</p>
</li>
<li><p>book类</p>
<p>public class Book {</p>
<pre><code>private String bookNo;
private String bookName;
private float bookPrice;

public Book() &#123;

&#125;

public Book(String bookNo,String bookName,float bookPrice)
&#123;
    this.bookName=bookName;
    this.bookNo=bookNo;
    this.bookPrice=bookPrice;
&#125;
</code></pre><p>}</p>
</li>
<li><p>CartItem类</p>
<p>public class CartItem {</p>
<pre><code>private Book book;
private int count;

public CartItem() &#123;
&#125;

public CartItem(Book book, int count) &#123;
    this.book = book;
    this.count = count;
&#125;

@Override
public  String toString()
&#123;
    return String.format(&quot;&quot;);
&#125;
</code></pre><p>}</p>
</li>
</ul>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%9B%9B%E7%AB%A0%20%E6%95%B0%E7%BB%84/" data-id="cl403sxrg0020f8vu3beh65za" data-title="" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%85%AD%E7%AB%A0%20java%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BC%96%E7%A8%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E4%B8%89%E7%AB%A0%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C#工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C++工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/Mysql/">Mysql</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/blender/">blender</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/linux/">linux</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/manim/">manim</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/spring/">spring</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C/">vba基础操作</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E5%88%9B%E9%80%A0%E6%A8%A1%E5%BC%8F/">创造模式</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">操作系统基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%8F%91/">操作系统开发</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E5%AD%A6/">数学</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">数据结构基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%A8%A1%E6%9D%BF/">模板</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%8B%B1%E8%AF%AD/">英语</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%99%9A%E5%B9%BB4/">虚幻4</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/">计算机组成原理</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/">计算机网络</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/notes-on-computer-expertise/tags/C/" rel="tag">C++</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/notes-on-computer-expertise/tags/C/" style="font-size: 10px;">C++</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/03/">March 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2021/12/">December 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/31/vue/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E9%AA%8C%E8%AF%81%E7%A0%81%E6%97%A0%E6%B3%95%E6%98%BE%E7%A4%BA/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/26/%E6%95%B0%E6%8D%AE%E5%BA%93/navicat%E5%AE%89%E8%A3%85/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/25/%E8%99%9A%E5%B9%BB4%E5%BC%95%E6%93%8E%E5%BC%80%E5%8F%91/%E8%99%9A%E5%B9%BB%E5%9B%9B%20mod%E5%88%B6%E4%BD%9C/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/python/python%E6%89%B9%E9%87%8F%E7%94%9F%E6%88%90%E6%95%B0%E6%8D%AE/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C%E7%AC%94%E8%AE%B0/EXCEL%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2022 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/notes-on-computer-expertise/" class="mobile-nav-link">Home</a>
  
    <a href="/notes-on-computer-expertise/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/notes-on-computer-expertise/js/jquery-3.4.1.min.js"></script>



  
<script src="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.js"></script>




<script src="/notes-on-computer-expertise/js/script.js"></script>





  </div>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>
</html>